using UnityEngine;
using System.Collections;

public class Traverse : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public bool each(System.Func<GameObject, bool> f) {
		if (f(gameObject)) return true;
		
		for (int i = 0; i < transform.childCount; i++) {
			GameObject child = transform.GetChild(i).gameObject;
			
			if (child.GetComponent<Traverse>() != null) {
				if (child.GetComponent<Traverse>().each(f)) return true;
			} else {
				if (f(child)) return true;
			}
		}
		
		return false;
	}
	
}

/*
#pragma strict

function each(f : function(GameObject)) {
	f(gameObject);
	
	for (var i : int = 0; i < transform.childCount; i += 1) {
		var obj : GameObject = transform.GetChild(i).gameObject;
		
		if (obj.GetComponent(Traversable) != null) {
			obj.GetComponent(Traversable).each(f);
		} else {
			f(obj);
		}
	}
}*/